const API = require("../../../utils/api")
const app = getApp()
// pages/park/park.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 1,
        detail: '',
        companyId: '',
        pageNum: 1,
        reachBottomShow: true,
        tabCurrent: 0,
        page: 'pages/park/company/company',
        canvasShow: false,
        articleList: [],
        employeePageNum: 1,
        employeeList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('onLoad------------------', options)
        //2. 获取启动参数 二维码
        if (options && options.scene) {
            //重置登陆状态
            app.globalData.userInfo = {}
            app.globalData.hasUserInfo = false

            let query = {}
            let scene = decodeURIComponent(options.scene).split('&')
            scene.forEach(ele => {
                let key = ele.split('=')[0]
                query[key] = ele.split('=')[1]
            })
            console.log('格式化启动参数', query)
            let globalQuery = {
                userId: query.uid,
                spreadType: query.st,
                type: query.t,
            }
            query.yqid ? globalQuery.gardenId = query.yqid : ''
            query.qyid ? globalQuery.enterpriseId = query.qyid : ''
            app.globalData.inviterUserId = query.uid
            app.globalData.qrQuery = globalQuery
        }

        //未认证的
        if (options && options.type == 2) {
            this.setData({
                type: 2,
            })
            this.get_company_business_information(options.id)
        } else {
            //认证的
            if (options && options.id) {
                this.setData({
                    companyId: options.id,
                })
            } else if (app.globalData.qrQuery && app.globalData.qrQuery.enterpriseId) {
                console.log('二维码参数', app.globalData.qrQuery)
                this.setData({
                    companyId: app.globalData.qrQuery.enterpriseId
                })
            }
            this.get_company_detail()
            this.get_compant_article_list()
        }
        this.get_employee_list()
    },
    contactEvent(event) {
        const { phone } = event.currentTarget.dataset.item
        wx.makePhoneCall({
            phoneNumber: phone
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // let that = this
        // let hasUserInfo = app.globalData.hasUserInfo
        // if (!hasUserInfo) {
        //     wx.reLaunch({
        //         url: '/pages/login/login?back=/pages/park/company/company&id=' + that.data.companyId,
        //     })
        // }
    },

    /**
     * 触底事件
     */
    onReachBottom: function () {
        if (this.data.reachBottomShow) {
            let pageNum = this.data.pageNum
            pageNum++
            this.setData({
                pageNum
            })
            // console.log('分页', articlePageNum)
            this.get_compant_article_list()
        }
    },

    /**
     * 接口 - 获取园区详情
     */
    get_company_detail: function () {
        let that = this
        const userInfo = wx.getStorageSync('userInfo')
        API.get_park_company_detail({
            id: that.data.companyId,
            wxUserid: userInfo.id,
        }).then(res => {
            if (res.code == 200) {
                let detail = res.data
                detail.honorCertification ? detail.honorCertification = detail.honorCertification.split(',') : ''
                detail.wxCompanyContacts.forEach(item => item.phoneText = item.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'))
                that.setData({
                    detail: detail
                })
            }
        })
    },

    /**
     * 接口 - 获取文章信息
     */
    get_compant_article_list: function () {
        let that = this
        API.get_compant_article_list({
            enterpriseId: that.data.companyId,
            pageNum: that.data.pageNum,
            pageSize: 10
        }).then(res => {
            if (res.code == 200) {
                that.setData({
                    articleList: [...that.data.articleList, ...res.rows]
                })
                if (res.total <= that.data.pageNum * 10) {
                    that.setData({
                        reachBottomShow: false
                    })
                }
            }
        })
    },

    /**
     * 接口 - 获取工商信息详情
     */
    get_company_business_information: function (id) {
        let that = this
        API.get_company_business_information({
            id: id,
        }).then(res => {
            that.setData({
                detail: res.data
            })
        })
    },

    /**
     * 查看文章详情
     */
    viewDetail: function (e) {
        wx.navigateTo({
            url: '/pages/share/detail/detail?type=2&id=' + e.currentTarget.dataset.item.id,
        })
    },

    /**
     * 事件 - 打开宣传手册
     */
    viewBrochure: function () {
        wx.downloadFile({
            url: this.data.detail.brochure,
            success: function (res) {
                const filePath = res.tempFilePath
                wx.openDocument({
                    filePath: filePath,
                    success: function (res) {
                        console.log('打开文档成功')
                    }
                })
            }
        })
    },

    /**
     * 事件 - 加微信
     */
    addWxFriend: function () {
        app.contact(this.data.detail.wxYard)
    },

    /**
     * 事件 - 电话联系
     */
    callPhone: function () {
        wx.makePhoneCall({
            phoneNumber: this.data.detail.phone
        })
    },

    /**
     * 路由 - 申请入驻
     */
    routeJoin: function () {
        if (!app.globalData.hasUserInfo) {
            wx.showToast({
                title: '您还没有登陆哦',
                icon: 'none'
            })

            setTimeout(() => {
                wx.reLaunch({
                    url: '/pages/login/login?back=/pages/park/company/company',
                })
            }, 1000)
            return
        }
        const { id, companyName } = this.data.detail
        wx.navigateTo({
            url: `/pages/park/company/edit/edit?type=3&companyId=${id}&companyName=${companyName}`,
        })
    },

    /**
     * 路由 - 名片
     */
    routeContact: function () {
        let that = this
        wx.navigateTo({
            url: '/pages/park/visiting-card/visiting-card?id=' + that.data.detail.cardId,
        })
    },

    /**
     * 路由 - 企业信息编辑
     */
    routeCompanyEdit: function (e) {
        wx.navigateTo({
            url: '/pages/park/company/edit/edit?type=2&id=' + this.data.detail.id,
        })
    },

    /**
     * 路由 - 企业管理
     */
    routeCompanyManage: function (e) {
        console.log(this.data.detail)
        wx.navigateTo({
            url: '/pages/mine/company-manage/company-manage?type=2&id=' + this.data.detail.id + '&cardId=' + this.data.detail.cardId
                + '&companyName=' + this.data.detail.company
        })
    },

    /**
     * 获取我的二维码
     */
    creat_wxmini_qrcode: function () {
        // if( !app.globalData.hasUserInfo ){
        //     wx.showToast({
        //         title: '您还没有登陆哦',
        //         icon:'none'
        //     })

        //     setTimeout( () => {
        //         wx.reLaunch({
        //             url: '/pages/login/login?back=/pages/park/park',
        //         })
        //     },1000)
        //     return
        // }
        this.setData({
            canvasShow: true,
            canvasId: 'shareImg'  //+ Date.parse(new Date())
        })
        console.log('打开组件画图', this.data.canvasId)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let that = this
        return {
            title: that.data.detail.company
        }
    },
    onShareTimeline: function () { },

    /**
     * 获取员工列表
     */
    get_employee_list: function () {
        let that = this
        API.get_employee_list({
            gardenId: that.data.companyId,
            pageNum: that.data.employeePageNum,
            pageSize: 3
        }).then(res => {
            console.log('员工', res)
            if (res.code == 200) {
                that.setData({
                    employeeList: [...that.data.employeeList, ...res.rows]
                })
                if (res.total <= that.data.employeePageNum * 10) {
                    that.setData({
                        reachBottomShow: false
                    })
                }
            }
        })
    },
})